package org.example.hansp.reflection.obtainc_class_structure_information;

import org.junit.jupiter.api.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ObtainClassStructure {
    Class<Person> personClass = Person.class;

    //获取全类名
    @Test
    public void test() {
        String name = personClass.getName();
        System.out.println(name);
    }
    //获取简单类名
    @Test
    public void test2() {
        String simpleName = personClass.getSimpleName();
        System.out.println(simpleName);
    }
    //获取所有public修饰的属性，包括本类和父类
    @Test
    public void test3() {
        Field[] fields = personClass.getFields();
        for (Field field : fields) {
            System.out.println(field.getName());
        }
    }
    //获取本类所有属性
    @Test
    public void test4() {
        Field[] declaredFields = personClass.getDeclaredFields();
        for (Field field : declaredFields) {
            System.out.println(field);
        }
    }
    //获取所有public方法，包括本类和父类
    @Test
    public void test5() {
        Method[] methods = personClass.getMethods();
        for (Method method : methods) {
            System.out.println(method.getName());
        }
    }
    //获取本类所有方法
    @Test
    public void test6() {
        Method[] declaredMethods = personClass.getDeclaredMethods();
        for (Method method : declaredMethods) {
            System.out.println(method.getName());
        }
    }
    //获取所有public构造器，包括本类不包含父类
    @Test
    public void test7() {
        Constructor<?>[] constructors = personClass.getConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.println(constructor);
        }
    }
    //获取本类中所有构造器
    @Test
    public void test8() {
        Constructor<?>[] declaredConstructors = personClass.getDeclaredConstructors();
        for (Constructor<?> constructor : declaredConstructors) {
            System.out.println(constructor);
        }
    }
    //以package形式返回包信息
    @Test
    public void test9() {
        Package aPackage = personClass.getPackage();
        System.out.println(aPackage.getName());
    }
    //以class形式返回父类信息
    @Test
    public void test10() {
        Class<? super Person> superclass = personClass.getSuperclass();
        System.out.println(superclass.getName());
    }
    //返回接口信息
    @Test
    public void test11() {
        Class<?>[] interfaces = personClass.getInterfaces();
        for (Class<?> anInterface : interfaces) {
            System.out.println(anInterface.getName());
        }
    }
    //返回注解信息
    @Test
    public void test12() {
        Annotation[] annotations = personClass.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
    }
}
